package com.zkzx.truck.service;

import com.zkzx.common.truck.model.TruckCertificatesVO;
import com.zkzx.truck.mapper.TruckCertificatesMapper;
import com.zkzx.truck.mapper.entity.TruckCertificates;
import com.zkzx.truck.mapper.entity.TruckCertificatesExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/11/17:35
 * @Description:
 */
@Slf4j
@Service
public class TruckCertificatesService {

    @Resource
    private TruckCertificatesMapper truckCertificatesMapper;

    /**
     * 根据车id查询证件信息
     * @param truckId 企业id
     * @return 证件信息
     */
    public TruckCertificatesVO findVOByTruckIdId(Integer truckId){

        TruckCertificates truckCertificates = findByTruckId(truckId);

        if(Objects.isNull(truckCertificates)){
            return null;
        }
        return TruckCertificatesVO.builder()
                .id(truckCertificates.getId())
                .truckId(truckCertificates.getTruckid())
                .office(truckCertificates.getOffice())
                .releaseTime(truckCertificates.getReleasetime())
                .regTime(truckCertificates.getRegtime())
                .drivingMainPic(truckCertificates.getDrivingmainpic())
                .drivingSubPic(truckCertificates.getDrivingsubpic())
                .transportNum(truckCertificates.getTransportnum())
                .transportValidTime(truckCertificates.getTransportvalidtime())
                .transportPic(truckCertificates.getTransportpic())
                .build();
    }

    /**
     * 根据车id查询证件信息
     * @param truckId 企业id
     * @return 证件信息
     */
    public TruckCertificates findByTruckId(Integer truckId){
        TruckCertificatesExample truckCertificatesExample = new TruckCertificatesExample();
        truckCertificatesExample.createCriteria().andTruckidEqualTo(truckId);
        Optional<TruckCertificates> truckCertificatesOne = truckCertificatesMapper.selectByExample(truckCertificatesExample).stream().findFirst();

        TruckCertificates truckCertificates  = null;
        if(truckCertificatesOne.isPresent()){
            truckCertificates = truckCertificatesOne.get();
        }
        return truckCertificates ;
    }

    /**
     * 插入证件信息
     * @param truckCertificatesVO 入参
     * @return 证件信息
     */
    public void insertOrUpdateByVO(TruckCertificatesVO truckCertificatesVO,Integer truckId){
        if (Objects.nonNull(truckCertificatesVO)){
            TruckCertificates truckCertificates = new TruckCertificates();
            truckCertificates.setTruckid(truckId);
            truckCertificates.setOffice(truckCertificatesVO.getOffice());
            truckCertificates.setReleasetime(truckCertificatesVO.getReleaseTime());
            truckCertificates.setRegtime(truckCertificatesVO.getRegTime());
            truckCertificates.setDrivingmainpic(truckCertificatesVO.getDrivingMainPic());
            truckCertificates.setDrivingsubpic(truckCertificatesVO.getDrivingSubPic());
            truckCertificates.setTransportnum(truckCertificatesVO.getTransportNum());
            truckCertificates.setTransportvalidtime(truckCertificatesVO.getTransportValidTime());
            truckCertificates.setTransportpic(truckCertificatesVO.getTransportPic());

            TruckCertificates byTruckId = this.findByTruckId(truckId);
            if(Objects.isNull(byTruckId)){
                truckCertificatesMapper.insert(truckCertificates);
                return;
            }
            TruckCertificatesExample truckCertificatesExample = new TruckCertificatesExample();
            truckCertificatesExample.createCriteria().andIdEqualTo(byTruckId.getId());

            truckCertificatesMapper.updateByExampleSelective(truckCertificates,truckCertificatesExample);

        }
    }


}
